﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using SD.Database.Classes;
using System.Collections.Generic;
using DBAccess;
namespace SD.Database
{
    public class Systems
    {
        public static SqlBoolean IsSystemInViewRange(int sId, int PlayerID)
        {
            int biology = UserFunctions.GetPlayerInt(PlayerID, "Biology");
            int OrginId = UserFunctions.GetPlayerInt(PlayerID, "OrginID");
            if (Distance(sId, OrginId) <= (int)(Math.Floor(biology / 2.0f)))
                return true;
            else
                return false;

        }
        public static int Distance(int sId1, int sId2)
        {
            List<int> t1 = SystemCoordinates(sId1);
            List<int> t2 = SystemCoordinates(sId2);
            return (int)Math.Sqrt(Math.Pow(t1[0] - t2[0], 2) + Math.Pow(t1[0] - t2[0], 2));

        }
        public static List<int> SystemCoordinates(int sid)
        {
            DataSet ds = DAL.GetInstance().GetData("SELECT * FROM Systems Where SystemID = @param0", new List<object>() { sid });
            if (ds.Tables.Count > 0)
                if (ds.Tables[0].Rows.Count > 0)
                    return new List<int>() { int.Parse(ds.Tables[0].Rows[0]["x"].ToString()), int.Parse(ds.Tables[0].Rows[0]["y"].ToString()) };

            return null;
        }

        public static List<int[]> GetAllSystemsFromBioRange(int sId, int bio)
        {
            List<int[]> tmp = new List<int[]>();
            List<int> cords = SystemCoordinates(sId);

            DataSet ds = DAL.GetInstance().GetData("Select x, y from Systems where  (SystemID <= (SELECT LastOrginID FROM Configuration))");

            int minX = cords[0] - bio / 2, maxX = cords[0] + bio / 2;
            int minY = cords[1] - bio / 2, maxY = cords[0] + bio / 2;

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                int x = int.Parse(row["x"].ToString());
                int y = int.Parse(row["y"].ToString());
                if (minX <= x && x <= maxX && minY <= y && y <= maxY)
                {
                    int[] t = new int[2] { x, y };
                    tmp.Add(t);
                }
            }
            return tmp;
        }
        public static string SystemName(int sId)
        {
            return DAL.GetInstance().GetScalar("Select SystemName From Systems where SystemID = @param0", new List<object>() { sId });
        }
        public static int SystemId(string name)
        {
            return DAL.GetInstance().GetInt("Select SystemID From Systems where SystemName Like @param0", new List<object>() { name });
        }
        public static int SystemAtCoordinates(int x, int y)
        {
            return DAL.GetInstance().GetInt("Select SystemID From Systems where X = @param0 and Y = @param1", new List<object>() { x, y });
        }
    };

}